package com.janlei.tdd.practise;

import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;

import static com.github.houbb.heaven.util.guava.Guavas.newArrayList;
import static com.github.houbb.heaven.util.guava.Guavas.newHashMap;
import static java.lang.String.valueOf;

public class FizzBuzzWhizz {

    public String report(int num) {
        return Rules.apply(num);
    }
}

class Rules {
    private final static List<Integer> FUNC = newArrayList();
    private final static Map<Integer, String> MAPPER = newHashMap();

    static {
        FUNC.addAll(newArrayList(3, 5, 7,11));
        MAPPER.put(3, "Fizz");
        MAPPER.put(5, "Buzz");
        MAPPER.put(7, "Whizz");
        MAPPER.put(11, "Tizz");
    }

    static String apply(int num) {
        StringBuilder out = new StringBuilder();

        BiFunction<Integer, Integer, Integer> biFun = Math::floorMod;
        BiFunction<Integer, BiFunction<Integer, Integer, Integer>, Boolean> mapFun = (cur, binFun) -> {
            Integer mod = binFun.apply(num, cur);
            return mod == 0;
        };

        FUNC.stream()
                .sorted()
                // num能否整除cur
                .filter(cur -> mapFun.apply(cur, biFun))
                // 获取规则映射数据
                .map(MAPPER::get)
                .forEach(out::append);

        if (out.length() == 0) {
            return valueOf(num);
        }

        return out.toString();
    }


}
